﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Farkhod.QuickDataViewer
{
    /// <summary>
    /// Interaction logic for DataWindow.xaml
    /// </summary>
    public partial class DataWindow : Window
    {
        public DataWindow()
        {
            InitializeComponent();
        }
        private string tvName = string.Empty;

        public DataWindow(string tableViewName)
        {
            InitializeComponent();
            if (!string.IsNullOrEmpty(tableViewName))
            {
                tvName = tableViewName;
                this.Title = tableViewName;
            }
        }

        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                await GetData();
            }
            catch (Exception ex)
            {
                ShowMessageInStatusBar(ex.Message);
            }
        }

        private async Task GetData()
        {
            try
            {
                DataTable dt = await DBManager.Instance.GetDataFromTable(tvName);
                grdData.ItemsSource = dt.AsDataView();
            }
            catch (Exception ex)
            {
                ShowMessageInStatusBar(ex.Message);
            }
        }

        private void ShowMessageInStatusBar(string message)
        {
            if (tbMessage.Text == string.Empty)
                tbMessage.Text = string.Format("{0}: {1}", "Error", message);
            else
                tbMessage.Text += string.Format("{0} {1}: {2}", "|| ", "Error", message);
        }

    }
}
